﻿using System.Windows;
using System.Windows.Input;
using WickedFlame.Core;
using WickedFlame.Core.Layers;
using WickedFlame.Util.Actions;
using WickedFlame.Util.Controls;

namespace WickedFlame.VecDraw.Plugins
{
    /// <summary>
    /// Interaction logic for LayerDisplay.xaml
    /// </summary>
	public partial class LayerDisplay : HeaderedToolItem
    {
        //DrawingCanvas _canvas;

        public LayerDisplay()
        {
            InitializeComponent();
        }

        private void ContentDocument_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Height > 266)
                this.Height = 266;
        }

        private void CommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            if (DrawingCanvas == null)
                return;

            if (e.Command == WickedFlame.VecDraw.VecApp.HistoryCommands.CombineLayersCommand)
            {
                MergeLayersAction action = new MergeLayersAction();
                action.DoAction(DrawingCanvas);

            }
            else if (e.Command == WickedFlame.VecDraw.VecApp.HistoryCommands.LayerDownCommand)
            {
                CanvasLayer layer = DrawingCanvas.SelectedLayer;
                DrawingCanvas.MoveLayerDown(layer);

            }
            else if (e.Command == WickedFlame.VecDraw.VecApp.HistoryCommands.LayerUpCommand)
            {
                CanvasLayer layer = DrawingCanvas.SelectedLayer;
                DrawingCanvas.MoveLayerUp(layer);

            }
            if (e.Command == WickedFlame.VecDraw.VecApp.HistoryCommands.AddCommand)
            {
                DrawingCanvas.AddLayer();
            }
            if (e.Command == WickedFlame.VecDraw.VecApp.HistoryCommands.DeleteCommand)
            {
                //RemoveLayer();
                DrawingCanvas.RemoveLayer();

            }
        }

        private void CommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (DrawingCanvas == null)
            {
                e.CanExecute = false;
                e.Handled = true;
                return;
            }

            if (e.Command == WickedFlame.VecDraw.VecApp.HistoryCommands.CombineLayersCommand ||
                e.Command == WickedFlame.VecDraw.VecApp.HistoryCommands.LayerDownCommand)
            {
                e.CanExecute = DrawingCanvas.SelectedLayerIndex < DrawingCanvas.Layers.Count - 1;
                e.Handled = true;
            }
            if (e.Command == WickedFlame.VecDraw.VecApp.HistoryCommands.LayerUpCommand)
            {
                e.CanExecute = DrawingCanvas.SelectedLayerIndex > 0;
                e.Handled = true;
            }
            if (e.Command == WickedFlame.VecDraw.VecApp.HistoryCommands.DeleteCommand)
            {
                e.CanExecute = DrawingCanvas.Layers.Count > 0;
                e.Handled = true;
            } 
            if (e.Command == WickedFlame.VecDraw.VecApp.HistoryCommands.AddCommand)
            {
            }

            if (!e.Handled)
            {
                e.CanExecute = true;
                e.Handled = true;
            }
        }
        
    }
}
